{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# List操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#初始化列表\n",
    "\n",
    "li = [1, 2, 3, 'abc', 4.5, [2, 3, 4], {1:'one'}]\n",
    "\n",
    "#获取长度\n",
    "print(len(li))\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "abc\n",
      "{1: 'one'}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 根据索引读写\n",
    "print(li[0])\n",
    "print(li[3])\n",
    "print(li[-1])#从最后的一个元素开始数\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 'a', 'b']\n",
      "[1, 2, 3, 'a', 'b', [4, 5, 6]]\n",
      "[1, 2, 3, 4, 5, 6]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 添加元素\n",
    "li = [1, 2, 3]\n",
    "li.append('a')\n",
    "li.append('b')\n",
    "print(li)\n",
    "li.append([4, 5, 6])\n",
    "print(li)\n",
    "li = [1, 2, 3]\n",
    "li.extend([4, 5, 6])\n",
    "print(li)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[2, 4]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 删除元素\n",
    "\n",
    "li = [1, 2, 3, 4, 5]\n",
    "li.pop()\n",
    "print(li)\n",
    "del(li[0])\n",
    "del(li[1])\n",
    "print(li)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "[1, 2, 3, 4, 5]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 元素是否存在\n",
    "\n",
    "li = [1, 2, 3, 4, 5]\n",
    "print(1 in li)\n",
    "print(6 in li)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empty\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 列表是否为空\n",
    "\n",
    "li = []\n",
    "if not li:\n",
    "    print('Empty')\n",
    "else:\n",
    "    print('Not Empty')\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 'd', 'E', 'F', 'g']\n",
      "abcdEFg\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 字符串\n",
    "s = 'abcdefg'\n",
    "li = list(s) #把字符串转为数组 便于修改\n",
    "li[4] = 'E'\n",
    "li[5] = 'F'\n",
    "print(li)\n",
    "s = ''.join(li)#数组转为字符串\n",
    "print(s)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 遍历\n",
    "li = [1, 2, 3]\n",
    "for i in li:\n",
    "    print(i)\n",
    "for i in  range(len(li)):#也可以用负数索引 表示从后向前遍历\n",
    "    print(li[i])\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'c': 3, 'b': 2, 'd': 4}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 初始化\n",
    "d = {'a':1, 'b':2, 'c':3, 'd':4}\n",
    "print(d)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 获取长度\n",
    "print(len(d))\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 100, 'c': 3, 'b': 2, 4: 'dd', 'd': 4}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 根据key读写\n",
    "d['a'] = 100\n",
    "d[4] = 'dd'\n",
    "print(d)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{4: 'd', 'c': 3}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 删除元素\n",
    "d = {'a':1, 2:'b', 'c':3, 4:'d'}\n",
    "del(d['a'])\n",
    "del(d[2])\n",
    "print(d)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a in d\n",
      "2 in d\n",
      "x not in d\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 判断key是否存在\n",
    "d = {'a':1, 2:'b', 'c':3, 4:'d'}\n",
    "if 'a' in d:\n",
    "    print('a in d')\n",
    "if 2 in  d:\n",
    "    print('2 in d')\n",
    "if not ('x' in d):\n",
    "    print('x not in d')\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d is empty\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 判断字典是否为空\n",
    "d = {}\n",
    "if not d:\n",
    "    print('d is empty')\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:1\n",
      "2:b\n",
      "4:d\n",
      "c:3\n",
      "a:1\n",
      "2:b\n",
      "4:d\n",
      "c:3\n"
     ]
    }
   ],
   "source": [
    "# 遍历\n",
    "d = {'a':1, 2:'b', 'c':3, 4:'d'}\n",
    "for k in d.keys():\n",
    "    print(str(k) + ':' + str(d[k]))\n",
    "for k,v in d.items():\n",
    "    print(str(k) + ':' + str(v))\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set([1, 2, 3, 4, 5])\n",
      "set([1, 2, 3, 4, 5])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 初始化\n",
    "s_a = set([1, 2, 3, 4, 5])\n",
    "s_b = set([1, 1, 2, 2, 3, 4, 5])\n",
    "print(s_a)\n",
    "print(s_b)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "5\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 获取长度\n",
    "print(len(s_a))\n",
    "print(len(s_b))\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set([1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 添加元素\n",
    "s_a.add(6)\n",
    "s_a.add(6)\n",
    "s_a.update([5,6,7,8,9])\n",
    "print(s_a)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set([1, 2, 3, 4, 5, 6, 7])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 删除元素\n",
    "s_a.remove(8)\n",
    "s_a.remove(9)\n",
    "print(s_a)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 判断元素是否存在\n",
    "print(1 in s_a)\n",
    "print(10 in s_a)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set is empty\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 判断集合是否为空\n",
    "s_a = set([])\n",
    "if not s_a:\n",
    "    print('set is empty')\n",
    "else:\n",
    "    print('set is not empty')\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 遍历\n",
    "s_a = set([1, 2, 3, 4, 5])\n",
    "for i in s_a:\n",
    "    print(i)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set([1, 2, 3, 4, 5, 6, 7, 8])\n",
      "set([1, 2, 3, 4, 5, 6, 7, 8])\n"
     ]
    }
   ],
   "source": [
    "# 集合操作\n",
    "s_a = set([1, 2, 3, 4, 5])\n",
    "s_b = set([4, 5, 6, 7, 8])\n",
    "# 并集\n",
    "print(s_a | s_b)\n",
    "print(s_a.union(s_b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set([4, 5])\n",
      "set([4, 5])\n"
     ]
    }
   ],
   "source": [
    "# 交集\n",
    "print(s_a & s_b)\n",
    "print(s_a.intersection(s_b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set([1, 2, 3])\n",
      "set([1, 2, 3])\n"
     ]
    }
   ],
   "source": [
    "# 差集 s_a - (s_a and s_b)\n",
    "print(s_a - s_b)\n",
    "print(s_a.difference(s_b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set([1, 2, 3, 6, 7, 8])\n",
      "set([1, 2, 3, 6, 7, 8])\n"
     ]
    }
   ],
   "source": [
    "# 对称差\n",
    "print(s_a ^ s_b)\n",
    "print(s_a.symmetric_difference(s_b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 容器切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 4]\n",
      "[0, 1, 2, 3]\n",
      "[5, 6, 7, 8, 9, 10]\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
      "[0, 2, 4]\n",
      "[3, 5, 7, 9]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "\n",
    "# 切片\n",
    "print(li[2:5])#2<=idx<5\n",
    "print(li[:4])\n",
    "print(li[5:])\n",
    "print(li[:])\n",
    "print(li[0:6:2])#\n",
    "print(li[3::2])#\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n",
      "[10, 8, 6, 4, 2, 0]\n",
      "[5, 6, 7, 8, 9]\n",
      "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n"
     ]
    }
   ],
   "source": [
    "# 负数索引和step\n",
    "print(li[::-1])#从后往前切\n",
    "print(li[::-2])\n",
    "print(li[-6:-1:1])\n",
    "print(li[-1::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表推倒"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n",
      "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n",
      "[0, 9, 36, 81]\n",
      "[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 一维数组\n",
    "print([i * 2 for i in range(10)])\n",
    "print([i * i for i in range(10)])\n",
    "print([i * i for i in range(10) if (i % 3) == 0])\n",
    "print([(x, y) for x in range(3) for y in range(3)])\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 3]\n",
      "[3, 3, 3]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 二维数组\n",
    "a = [[3] * (i + 1) for i in range(3)]\n",
    "print(a[1])\n",
    "print(a[2])\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 100, 3], [1, 100, 3], [1, 100, 3]]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 乘法问题\n",
    "a = [[1,2,3]] * 3\n",
    "a[1][1] = 100\n",
    "print(a)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], [1, 100, 3], [1, 2, 3]]\n"
     ]
    }
   ],
   "source": [
    "# 解决方法\n",
    "a = [[1,2,3] for i in range(3)]\n",
    "a[1][1] = 100\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 生成器（用到的时候才计算）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<type 'list'>\n",
      "<type 'list'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\nprint(next(sx))\\nprint(next(sx))\\nprint(next(sx))\\nprint(next(sx))\\nprint(next(sx))\\nprint(next(sx))\\nprint(next(sx))\\nprint(next(sx))\\nprint(next(sx))\\nprint(next(sx))\\n\\n'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 简单方法\n",
    "x = range(10)\n",
    "print(type(x))\n",
    "sx = (x * x for x in range(10))\n",
    "print(type(x))\n",
    "'''\n",
    "print(next(sx))\n",
    "print(next(sx))\n",
    "print(next(sx))\n",
    "print(next(sx))\n",
    "print(next(sx))\n",
    "print(next(sx))\n",
    "print(next(sx))\n",
    "print(next(sx))\n",
    "print(next(sx))\n",
    "print(next(sx))\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "4\n",
      "9\n",
      "16\n",
      "25\n",
      "36\n",
      "49\n",
      "64\n",
      "81\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for i in sx:\n",
    "    print(i)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n",
      "55\n"
     ]
    }
   ],
   "source": [
    "# 生成器实现fib数列\n",
    "def fib(limit):\n",
    "    n,a,b = 0,0,1\n",
    "    while n < limit:\n",
    "        yield b #第一次返回\n",
    "        a, b =  b, a + b\n",
    "        n += 1\n",
    "for i in fib(10):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 面向对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "hello: 100\n",
      "\n",
      "77495496\n",
      "77495496\n",
      "hey: 100\n"
     ]
    }
   ],
   "source": [
    "# 类成员及方法\n",
    "class MyClass:#继承 object 成员变量默认是public类型\n",
    "    def __init__(self, val):#初始化函数 注意并不是构造函数\n",
    "        self.val = val\n",
    "    \n",
    "    def display(self, s):\n",
    "        print('%s: %d' % (s,self.val))\n",
    "        \n",
    "m = MyClass(100)\n",
    "print(m.val)\n",
    "m.display('hello')#=>display(m,'hello')\n",
    "print('')\n",
    "\n",
    "m2 = m\n",
    "print(id(m))\n",
    "print(id(m2))\n",
    "\n",
    "fn = m.display\n",
    "fn('hey')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 继承\n",
    "class Base:\n",
    "    pass\n",
    "\n",
    "class MyClass(Base):\n",
    "    def run(self):\n",
    "        print('MyClass run')\n",
    "        \n",
    "class MyClass2(Base):\n",
    "    def run(self):\n",
    "        print('MyClass2 run')\n",
    "        \n",
    "m = MyClass()\n",
    "print(isinstance(m,MyClass))\n",
    "print(issubclass(MyClass2,Base))\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MyClass run\n",
      "MyClass2 run\n",
      "\n",
      "man run\n",
      "monkey run\n"
     ]
    }
   ],
   "source": [
    "# 多态\n",
    "m2 = MyClass2()\n",
    "m.run()\n",
    "m2.run()\n",
    "print('')\n",
    "\n",
    "def duck(d):\n",
    "    d.run()\n",
    "    \n",
    "class Man:\n",
    "    def run(self):\n",
    "        print('man run')\n",
    "        \n",
    "class Monkey:\n",
    "    def run(self):\n",
    "        print('monkey run')\n",
    "        \n",
    "duck(Man())\n",
    "duck(Monkey())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 异常处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "try...\n",
      "('ZeroDivisionError:', ZeroDivisionError('integer division or modulo by zero',))\n",
      "None\n",
      "finally...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"<ipython-input-66-327b04270746>\", line 5, in <module>\n",
      "    r = 10 / 0\n",
      "ZeroDivisionError: integer division or modulo by zero\n"
     ]
    }
   ],
   "source": [
    "import traceback\n",
    "\n",
    "try:\n",
    "    print('try...')\n",
    "    r = 10 / 0\n",
    "except ZeroDivisionError as e:\n",
    "# 2.7: except ZeroDivisionError, e:\n",
    "    print('ZeroDivisionError:',e)\n",
    "    print(traceback.print_exc())\n",
    "finally:\n",
    "    print('finally...')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文件IO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u0000\u0010\b\u0000\n",
      "\u0000�@@\n",
      "(10240064, 31872)\n"
     ]
    }
   ],
   "source": [
    "import struct\n",
    "\n",
    "# pack与unpack\n",
    "# '>'代表big-endian编码\n",
    "# 'I'代表4个字节的整数\n",
    "# 'H'代表2字节无符号整数\n",
    "print(struct.pack('>I',1050624))\n",
    "print(struct.pack('>I',10240064))\n",
    "print(struct.unpack('>IH',b'\\x00\\x9c\\x40\\x40\\x7c\\x80'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('\\x89', 'P', 168642382, 2586, 1212747008, 21060, 60420, 101238017, 0, 16640)\n",
      "图片大小: 168642382\n",
      "实际图像偏移量: 1212747008\n",
      "header字节数: 60420\n",
      "高度: 60420\n",
      "宽度: 101238017\n",
      "颜色数: 16640\n"
     ]
    }
   ],
   "source": [
    "# 处理二进制文件\n",
    "import struct\n",
    "\n",
    "with open('sample.jpg','rb') as f:\n",
    "    header = struct.unpack('<2c6I2H',f.read(30))\n",
    "    print(header)\n",
    "    print('图片大小: %d' % header[2])\n",
    "    print('实际图像偏移量: %d' % header[4])\n",
    "    print('header字节数: %d' % header[6])\n",
    "    print('高度: %d' % header[6])\n",
    "    print('宽度: %d' % header[7])\n",
    "    print('颜色数: %d' % header[9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"Python\": 100, \"Others\": {\"C\": 65, \"Java\": 50}, \"C++\": 70, \"Basic\": 60}\n",
      "{u'Python': 100, u'Basic': 60, u'C++': 70, u'Others': {u'C': 65, u'Java': 50}}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "d = {'Python':100,'C++':70,'Basic':60,'Others':{'C':65,'Java':50}}\n",
    "jtxt = json.dumps(d)\n",
    "dd = json.loads(jtxt)\n",
    "print(jtxt)\n",
    "print(dd)\n",
    "print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom: 15, 85\n",
      "{'age': 15, 'score': 85, 'name': 'Tom'}\n",
      "{\"age\": 15, \"score\": 85, \"name\": \"Tom\"}\n",
      "Tom: 15, 85\n",
      "{\"age\": 15, \"score\": 85, \"name\": \"Tom\"}\n"
     ]
    }
   ],
   "source": [
    "# 非dict对象如何用json序列化?\n",
    "class Student:\n",
    "    def __init__(self,name,age,score):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.score = score\n",
    "        \n",
    "    def __str__(self):\n",
    "        return '%s: %d, %d' % (self.name,self.age,self.score)\n",
    "    \n",
    "s = Student('Tom', 15, 85)\n",
    "print(s)\n",
    "print(s.__dict__)\n",
    "# 方法1\n",
    "jtxt = json.dumps(s,default = lambda obj: obj.__dict__)\n",
    "print(jtxt)\n",
    "def d2s(d):\n",
    "    return Student(d['name'],d['age'],d['score'])\n",
    "print(json.loads(jtxt,object_hook = d2s))\n",
    "# 方法2\n",
    "def s2d(s):\n",
    "    return s.__dict__\n",
    "jtxt = json.dumps(s,default = s2d)\n",
    "print(jtxt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python2与3的区别"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "xrange（为了内存利用率 xrange会动态分配内存）、print（3中一定要加括号）、异常（2中except ZeroDivisionError as e 比3多了as ）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
